<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@include file="/ui/jsp/includes/header.jspf"%>
<script type="text/javascript">
function addNewThesis()
{
	$('#theisForm').attr('action', 'addNewThesis.html');
	$('#theisForm').submit();
}
function updateThesis()
{
	$('#thesisId').val('${thesis.thesisId}');
	$('#theisForm').attr('action', 'updateThesis.html');
	$('#theisForm').submit();
}
function backToThesisList()
{
	$('#theisForm').attr('action', 'viewThesisList.html');
	$('#theisForm').submit();
}

</script>
<%-- <c:set var="isProspectusCompleted" scope="page" >${thesis.prospectusCompleted}</c:set> --%>


<div class="hero-unit secondary_container">
    <div id="table-subject-header">
        <c:if test="${!empty thesis.thesisId}">
            Update Thesis
        </c:if>
        <c:if test="${empty thesis.thesisId}">
            Add New Thesis
        </c:if>
    </div>
    <hr>

    <div class="row-fluid">
        <form:form method="POST" id="theisForm" action="addNewThesis.html" commandName="thesis" class="form-horizontal">
        	<div id="errorDiv" class="errorblock"></div>
            <div class="basicthesisform">
                <span id="table-subject-header">Basic Thesis Form</span><hr>
                <div class="control-group">
                    <label class="control-label" for="title">Thesis Title*</label> 
                    <div class="controls">       
                        <form:input id="title" path="title"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="abstract_">Thesis Abstract</label>
                    <div class="controls"> 
                        <form:textarea id="abstract_" rows="20" path="abstract_" cssStyle="height:60px"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="studentName">Student Name*</label> 
                    <div class="controls">    
                        <input type="text" name="studentName" id="studentName"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="categoryStr">Thesis Category*</label>
                    <div class="controls">
                    	<c:choose>
                    	<c:when test="${not empty thesis.category.categoryId}">
                    	<form:select id="categoryStr" path="categoryStr" >
                            <form:option value="${thesis.category.categoryId}" label="${thesis.category.categoryName}"/>
                            <form:options  items="${categoryList}" itemValue="categoryId" itemLabel="categoryName" />
                        </form:select>
                    	</c:when>
                    	<c:otherwise>
                    	<form:select id="categoryStr" path="categoryStr">
                            <form:option value="" label="Select" />
                            <form:options items="${categoryList}" itemValue="categoryId" itemLabel="categoryName" />
                        </form:select>
                    	</c:otherwise>
                    	</c:choose>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="thesisDoi">DOI (Digital Object Identifier)</label>
                    <div class="controls">
                        <form:input id="thesisDoi" path="thesisDoi"/>
                    </div>
                </div>
                <div class="control-group">
                    <div class="controls">
                        <button Id="button1" class="btn btn-primary" type="button">Continue</button>
                        <input type="button" value="Cancel" onclick="javascript:backToThesisList()" class="btn btn-primary"/>
                    </div>
                </div>
            </div>

            <div style="display:none" class="thesiscommitteeform">
                <span id="table-subject-header">Thesis Committee</span><hr>
                <div class="control-group">
                    <label class="control-label" for="thesisChairId">Thesis Chair*</label>
                    <div class="controls">
                    	<c:choose>
                    	<c:when test="${not empty thesis.thesisChairId}">
                    	<form:select id="thesisChairId" path="thesisChairId" >
                            <form:option value="${thesis.thesisChairId}" label="${thesis.thesisChairName}"/>
                            <form:option value="" label="Select" />
                             <form:options items="${facultyList}" itemValue="facultyId" itemLabel="fullName" />
                        </form:select>
                    	</c:when>
                    	<c:otherwise>
                    	<form:select id="thesisChairId" path="thesisChairId">
                            <form:option value="" label="Select" />
                            <form:options items="${facultyList}" itemValue="facultyId" itemLabel="fullName" />
                        </form:select>
                    	</c:otherwise>
                    	</c:choose>
                    	
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="committeeMember1Id">Committee Member 1</label>
                    <div class="controls">
                    	<c:choose>
                    	<c:when test="${not empty thesis.committeeMember1Id}">
                    	<form:select id="committeeMember1Id" path="committeeMember1Id" >
                            <form:option value="${thesis.committeeMember1Id}" label="${thesis.committeeMember1Name}"/>
                            <form:option value="" label="Select" />
                             <form:options items="${facultyList}" itemValue="facultyId" itemLabel="fullName" />
                        </form:select>
                    	</c:when>
                    	<c:otherwise>
                    	<form:select id="committeeMember1Id" path="committeeMember1Id">
                            <form:option value="" label="Select"/>
                            <form:options items="${facultyList}" itemValue="facultyId" itemLabel="fullName" />
                        </form:select>
                    	</c:otherwise>
                    	</c:choose>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="committeeMember2Id">Committee Member 2</label>
                    <div class="controls">
                    	<c:choose>
                    	<c:when test="${not empty thesis.committeeMember2Id}">
                    	<form:select id="committeeMember2Id" path="committeeMember2Id" >
                            <form:option value="${thesis.committeeMember2Id}" label="${thesis.committeeMember2Name}"/>
                            <form:option value="" label="Select" />
                             <form:options items="${facultyList}" itemValue="facultyId" itemLabel="fullName" />
                        </form:select>
                    	</c:when>
                    	<c:otherwise>
                    	<form:select id="committeeMember2Id" path="committeeMember2Id">
                            <form:option value="" label="Select" />
                            <form:options items="${facultyList}" itemValue="facultyId" itemLabel="fullName" />
                        </form:select>
                    	</c:otherwise>
                    	</c:choose>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="committeeMember3Id">Committee Member 3</label>
                    <div class="controls">
                    	<c:choose>
                    	<c:when test="${not empty thesis.committeeMember3Id}">
                    	<form:select id="committeeMember3Id" path="committeeMember3Id" >
                            <form:option value="${thesis.committeeMember3Id}" label="${thesis.committeeMember3Name}"/>
                            <form:option value="" label="Select" />
                             <form:options items="${facultyList}" itemValue="facultyId" itemLabel="fullName" />
                        </form:select>
                    	</c:when>
                    	<c:otherwise>
                    	<form:select id="committeeMember3Id" path="committeeMember3Id">
                            <form:option value="" label="Select" />
                            <form:options items="${facultyList}" itemValue="facultyId" itemLabel="fullName" />
                        </form:select>
                    	</c:otherwise>
                    	</c:choose>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="committeeMember4Id">Committee Member 4</label>
                    <div class="controls">
                    	<c:choose>
                    	<c:when test="${not empty thesis.committeeMember4Id}">
                    	<form:select id="committeeMember4Id" path="committeeMember4Id" >
                            <form:option value="${thesis.committeeMember4Id}" label="${thesis.committeeMember4Name}"/>
                            <form:option value="" label="Select" />
                             <form:options items="${facultyList}" itemValue="facultyId" itemLabel="fullName" />
                        </form:select>
                    	</c:when>
                    	<c:otherwise>
                    	<form:select id="committeeMember4Id" path="committeeMember4Id">
                            <form:option value="" label="Select" />
                            <form:options items="${facultyList}" itemValue="facultyId" itemLabel="fullName" />
                        </form:select>
                    	</c:otherwise>
                    	</c:choose>
                    </div>
                </div>
                <div class="control-group">
                    <div class="controls">
                        <button Id="button4" class="btn btn-primary" type="button">Back</button>
                        <button Id="button2" class="btn btn-primary" type="button">Continue</button>
                        <input type="button" value="Cancel" onclick="javascript:backToThesisList()" class="btn btn-primary"/>
                    </div>
                </div>
            </div>

            <div style="display:none" class="thesisstatusform">
<%--             <c:set var="enrolled" scope="page">${thesis.enrolled}</c:set> --%>
<%-- <c:set var="enrolledDate" scope="page">${thesis.enrolledDate}</c:set> --%>
<%-- <c:set var="prospectusPassed" scope="page">${thesis.prospectusPassed}</c:set> --%>
<%-- <c:set var="prospectusDate" scope="page">${thesis.prospectusDate}</c:set> --%>
<%-- <c:set var="defensePassed" scope="page">${thesis.defensePassed}</c:set> --%>
<%-- <c:set var="defenseDate" scope="page">${thesis.defenseDate}</c:set> --%>
<!--                <span id="table-subject-header">Thesis Status</span><hr> -->
           		
           		<div class="control-group">               
                    <label class="control-label">Enrolled in Thesis?</label>
                    <div class="controls">
                        <form:radiobutton path="enrolled" value="Y" /> Yes
                        <form:radiobutton path="enrolled" value="N" /> No
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Enrollment Date</label>
                    <div class="controls">
                        <form:input id="enrolledDate" path="enrolledDateStr"/>
                    </div>
                </div>
                <div class="control-group">               
                    <label class="control-label">Thesis Prospectus passed?</label>
                    <div class="controls">
                        <form:radiobutton path="prospectusPassed" value="Y" /> Yes
                        <form:radiobutton path="prospectusPassed" value="N" /> No
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Prospectus Date</label>
                    <div class="controls">
                        <form:input id="prospectusDate" path="prospectusDateStr"/>
                    </div>
                </div>
                
                <div class="control-group">
                    <label class="control-label">Thesis Defense Passed?</label>
                    <div class="controls">
                        <form:radiobutton path="defensePassed" value="Y" /> Yes
                        <form:radiobutton path="defensePassed" value="N" /> No
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="defenseDate">Defense Date</label>
                    <div class="controls">
                        <form:input id="defenseDate" path="defenseDateStr"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Submitted to the SoC Director?</label>
                    <div class="controls">
                        <form:radiobutton path="SubmittedDirector" value="Y" /> Yes
                        <form:radiobutton path="SubmittedDirector" value="N" /> No
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="SubmittedDirectorDate">Submitted Date</label>
                    <div class="controls">
                        <form:input id="subDirDate" path="subDirDateStr"/>
                    </div>
                </div><div class="control-group">
                    <label class="control-label" for="acceptedDirectorDate">Accepted Date</label>
                    <div class="controls">
                        <form:input id="acpDirDate" path="acpDirDateStr"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Submitted to the CCEC Dean?</label>
                    <div class="controls">
                        <form:radiobutton path="SubmittedDean" value="Y" /> Yes
                        <form:radiobutton path="SubmittedDean" value="N" /> No
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="submittedDeanDate">Submitted Date</label>
                    <div class="controls">
                        <form:input id="subDeanDate" path="subDeanDateStr"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="acceptedDeanDate">Accepted Date</label>
                    <div class="controls">
                        <form:input id="acpDeanDate" path="acpDeanDateStr"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Submitted to the Graduate School Dean?</label>
                    <div class="controls">
                        <form:radiobutton path="SubmittedGradDean" value="Y" /> Yes
                        <form:radiobutton path="SubmittedGradDean" value="N" /> No
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="submittedGradDeanDate">Submitted Date</label>
                    <div class="controls">
                        <form:input id="subGradDeanDate" path="subGradDeanDateStr"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="acceptedGradDeanDate">Accepted Date</label>
                    <div class="controls">
                        <form:input id="acpGradDeanDate" path="acpGradDeanDateStr"/>
                    </div>
                </div>
                
                <div class="control-group">
                    <label class="control-label">Submitted to the SoC Library?</label>
                    <div class="controls">
                        <form:radiobutton path="SubmittedLibrary" value="Y" /> Yes
                        <form:radiobutton path="SubmittedLibrary" value="N" /> No
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="submittedLibraryDate">Submitted Date</label>
                    <div class="controls">
                        <form:input id="subLibraryDate" path="subLibraryDateStr"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="acceptedLibraryDate">Accepted Date</label>
                    <div class="controls">
                        <form:input id="acceptedLibraryDate" path="acpLibraryDateStr"/>
                    </div>
                </div>
                
                <div class="control-group">
                    <label class="control-label" for="comments">Comments
                    </label>
                    <div class="controls">
                        <form:textarea path="comments"/>
                    </div>
                </div>
                <div class="control-group">
                    <div class="controls">
                        <button id="button3" class="btn btn-primary" type="button">Back</button>
                    	<c:choose>
		                    <c:when test="${!empty thesis.thesisId}">
		                     <form:hidden path="thesisId" id="thesisId"/>
		                    <input type="button" onclick="javascript:updateThesis()" value="Submit" class="btn btn-primary"/>
		                    </c:when>
		                    <c:otherwise>
		                    	<input type="button" onclick="javascript:addNewThesis()" value="Submit" class="btn btn-primary"/>
		                    </c:otherwise>
		                    </c:choose>
		                    <input type="button" value="Cancel" onclick="javascript:backToThesisList()" class="btn btn-primary"/>                                 
                    </div>
                </div>
            </div>
            <form:hidden id="thesis_student" path="student.studentId"/>
        </form:form>
    </div>
</div>

<%@include file="/ui/jsp/includes/footer.jspf"%>

<script type="text/javascript">
    $(function() {
        var studentList = '${studentList}';
        //alert(studentList);
        var studentStr = studentList.substring(studentList.indexOf('{') + 1, studentList.indexOf('}'));
        studentStr = studentStr.replace(/=/g, ":");
        var studentArr = studentStr.split(',');
        var tempArray = new Array();
        var tempStr = "";
        for (var i = 0; i < studentArr.length; i++) {
            tempStr = studentArr[i];
            tempStr = tempStr.substring(tempStr.indexOf(":") + 1);
            tempArray[i] = tempStr;
        }
        $("#studentName").autocomplete({
            source: tempArray,
            select: function(e, ui) {
                //create formatted data
                var student = ui.item.value;
                getId(student, studentArr);
                $("#studentName").val($.trim(student));
            },
            close: function(event, ui) {
            }
        });

    });
    function getId(str, arr) {
        var key = "";
        var value = "";
        var tempStr = "";
        for (var i = 0; i < arr.length; i++) {
            tempStr = arr[i];
            tempStr = tempStr.replace(/^\s+|\s+$/g, '');
            key = tempStr.substring(0, tempStr.indexOf(":"));
            value = tempStr.substring(tempStr.indexOf(":") + 1);
            if (value == str) {
                $("#thesis_student").val($.trim(key));
            }
        }
    }

    $(document).ready(function() {
    	$('#errorDiv').hide();
    	
    	$("#button1").click(function(){
    		var errorFlag = false;
    		$('#errorDiv').empty();	
        	if($('#title').val()=="")
        	{
        		errorFlag = true;
        		$('#errorDiv').html("Thesis Title is a required field.<br/>");
        	}
        	if($('#studentName').val()=="")
        	{
        		errorFlag = true;
        		$("#thesis_student").val("");
        		$('#errorDiv').append("Student Name is a required field.<br/>");
        	}
        	if($('#thesis_student').val()=="")
        	{
        		errorFlag = true;
        		$('#errorDiv').append("Please choose from the Auto-complete Dropdown List to choose a Student.<br/>");
        	}
        	else{
        		
        		var idMap = '${idMap}';
                var idStr = idMap.substring(idMap.indexOf('{') + 1, idMap.indexOf('}'));
                idStr = idStr.replace(/=/g, ":");
                var idMapArr = idStr.split(',');
                var tempArray = new Array();
                var tempStr = "";
                
                for (var i = 0; i < idMapArr.length; i++) {
                    tempStr = idMapArr[i];
                    tempStr = tempStr.substring(tempStr.indexOf(":") + 1);
                    tempArray[i] = tempStr;
                }
                
                for (var j = 0; j < tempArray.length; j++){
                	if($('#thesis_student').val() === tempArray[j]){
                		errorFlag = true;
                		$('#errorDiv').append("This student already has a thesis entry.<br/>");
                	}
                }
        	}
        	if($('#categoryStr').val()=="")
        	{
        		errorFlag = true;
        		$('#errorDiv').append("Thesis Category is a required field.<br/>");
        	}
        	if(errorFlag==true)
        		$('#errorDiv').show();
        	if(errorFlag==false)
        	{
        		$('#errorDiv').hide();
            	$(".basicthesisform, .thesiscommitteeform").toggle();
        	}
        });
        $("#button2").click(function() {
        	var errorFlag = false;
        	
        	if(($('#thesisChairId').val()==($('#committeeMember1Id').val())  && $('#thesisChairId').val()!="")|| 
        			($('#thesisChairId').val()==($('#committeeMember2Id').val())  && $('#thesisChairId').val()!="")||
        			($('#thesisChairId').val()==($('#committeeMember3Id').val())  && $('#thesisChairId').val()!="")||
        			($('#thesisChairId').val()==($('#committeeMember4Id').val())  && $('#thesisChairId').val()!="")||
        			($('#committeeMember1Id').val()==($('#committeeMember2Id').val())  && $('#committeeMember1Id').val()!="")|| 
        			($('#committeeMember1Id').val()==($('#committeeMember3Id').val())  && $('#committeeMember1Id').val()!="")||
        			($('#committeeMember1Id').val()==($('#committeeMember4Id').val())  && $('#committeeMember1Id').val()!="")||
        			($('#committeeMember2Id').val()==($('#committeeMember3Id').val())  && $('#committeeMember2Id').val()!="")|| 
        			($('#committeeMember2Id').val()==($('#committeeMember4Id').val())  && $('#committeeMember2Id').val()!="")||
        			($('#committeeMember3Id').val()==($('#committeeMember4Id').val()) && $('#committeeMember3Id').val()!="")){
        		errorFlag = true;
        		$('#errorDiv').append("A single Faculty Member cannot be used twice.");
        	}
        	if($('#thesisChairId').val()==""){
        		errorFlag = true;
        		$('#errorDiv').append("A Thesis Chair Advisor must be specified.");
        	}
        	
        	if(errorFlag==true)
        		$('#errorDiv').show();
        	if(errorFlag==false)
        	{
        		$('#errorDiv').hide();
            	$(".thesiscommitteeform, .thesisstatusform").toggle();
        	}
        });
        $("#button3").click(function() {
            $(".thesiscommitteeform, .thesisstatusform").toggle();
        });
        $("#button4").click(function() {
            $(".thesiscommitteeform, .basicthesisform").toggle();
        });
        $("#studentName").val('${thesis.studentFullName}');
    });
    
    $(function() {
        $("#enrolledDate").datepicker();
      });
    $(function() {
        $("#prospectusDate").datepicker();
      });
    $(function() {
        $("#defenseDate").datepicker();
      });
    $(function() {
        $("#subDirDate").datepicker();
      });
    $(function() {
        $("#acpDirDate").datepicker();
      });
    $(function() {
        $("#subDeanDate").datepicker();
      });
    $(function() {
        $("#acpDeanDate").datepicker();
      });
    $(function() {
        $("#subGradDeanDate").datepicker();
      });
    $(function() {
        $("#acpGradDeanDate").datepicker();
      });
    $(function() {
        $("#subLibraryDate").datepicker();
      });
    $(function() {
        $("#acceptedLibraryDate").datepicker();
      });
    
</script>

